package com.miao.manage.web.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.miao.entity.PageVo;
import com.miao.entity.TbItem;
import com.miao.entity.TbItemCat;
import com.miao.service.TbItemCatService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 * 商品类目 前端控制器
 * </p>
 *
 * @author shuangshuang
 * @since 2018-09-07
 */
@Controller
@RequestMapping("/tbItemCat")
public class TbItemCatController {

    //日志
    private static final Logger LOGGER = LoggerFactory.getLogger(TbItemCatController.class);

    @Autowired
    private TbItemCatService tbItemCatService;

    /**
     * 查询商品类表类目
     * @param pid 父节点  id 子节点
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<List<TbItemCat>> queryitemcat(@RequestParam(value = "id",defaultValue = "0") Long pid){
        try {
            EntityWrapper<TbItemCat> wrapper = new EntityWrapper<>();
            wrapper.where("parent_id = {0}", pid);
            List<TbItemCat> tbItemCats = tbItemCatService.selectList(wrapper);
            if(null==tbItemCats||tbItemCats.isEmpty()){
               return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
           }
           return ResponseEntity.ok(tbItemCats);
        }catch (Exception e){
            LOGGER.error("查询商品类表类目出错了！",e);
        }
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
    }


    /**
     * 根据类目ID查询类目名称，用于数据回显
     * @param cid
     * @return
     */
    @RequestMapping("/queryName")
    public ResponseEntity<TbItemCat> queryName(Long cid){
        try{

            EntityWrapper<TbItemCat> wrapper = new EntityWrapper<>();
            wrapper.where("id={0}",cid);
            TbItemCat tbItemCat = tbItemCatService.selectOne(wrapper);
            return  ResponseEntity.ok(tbItemCat);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null);
    }

}

